<?php
	require("Config.class.php");
	//require("DBHelper.class.php");

	class WXHelper
	{
		public function validate()
		{ 
			$echoStr = $_GET["echostr"];			
			$signature = $_GET["signature"];
			$timestamp = $_GET["timestamp"];
			$nonce = $_GET["nonce"];
			$token = TOKEN;
			
			$tmpArr = array($token, $timestamp, $nonce);
			sort($tmpArr);
			$tmpStr = implode( $tmpArr );
			$tmpStr = sha1($tmpStr);
			
			if( $tmpStr == $signature ){
				echo $echoStr;
				exit;
			}
		}
		
		public function processRequest()
		{
			//get post data, May be due to the different environments
			$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
			//extract post data
			if (!empty($postStr))
			{
				$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
				
				$fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
				$msgType = $postObj->MsgType;
				
				switch($msgType)
				{
					case "text":
						$keyword = trim($postObj->Content);
						$this->processTextMessage($fromUsername, $toUsername, $keyword);
					break;
					case "location":
						$locationX = $postObj->Location_X;
						$locationY = $postObj->Location_Y;
						$this->processLocationMessage($fromUsername, $toUsername, $locationX, $locationY);
					break;
					default:
						echo $this->generateMessage($fromUsername, $toUsername, ERROR_MESSAGE);	
					break;
				}
			}
			else 
			{
				echo $this->generateMessage($fromUsername, $toUsername, ERROR_MESSAGE);	
			}
		}
		
		private function processTextMessage($fromUsername, $toUsername, $keyword)
		{
			if(!empty($keyword))
			{
				switch($keyword)
				{
					case "subscribe":
					case "Hello2BizUser":
						echo $this->generateMessage($fromUsername, $toUsername, WELCOME_MESSAGE);
					break;
                  	case "h":
					case "help":
						echo $this->generateMessage($fromUsername, $toUsername, HELP_MESSAGE);
					break;
					default:
						$this->getRoute($fromUsername, $toUsername, $keyword);
					break;
				}
			}
			else
			{
				echo $this->generateMessage($fromUsername, $toUsername, ERROR_MESSAGE);			
			} 
		}
		
		private function getRoute($fromUsername, $toUsername, $keyword)
		{
			//$dbHelper = new DBHelper();
			//$currentLocation = $dbHelper->getLocationByUsername($fromUsername);

			//if(!empty($currentLocation))
			//{
			//	echo $this->generateMessage($fromUsername, $toUsername, $currentLocation);
			//}
			//else
			//{
			//	echo $this->generateMessage($fromUsername, $toUsername, LOCATION_REQUIRED_MESSAGE);
			//}			
		}
		
		private function processLocationMessage($fromUsername, $toUsername, $locationX, $locationY)
		{
			//$dbHelper = new DBHelper();
			//$location = sprintf("%s|%s", $locationX, $locationY);
			//$dbHelper->saveLocationByUsername($fromUsername, $location);

			//echo $this->generateMessage($fromUsername, $toUsername, LOCATION_READY_MESSAGE);
		}
		
		private function generateMessage($fromUsername, $toUsername, $contentStr)
		{
			$time = time();
			$textTpl = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[text]]></MsgType>
						<Content><![CDATA[%s]]></Content>
						<FuncFlag>0</FuncFlag>
						</xml>";
			return sprintf($textTpl, $fromUsername, $toUsername, $time, $contentStr);
		}
	}
?>